<?php
// +----------------------------------------------------------------------
// | HkCms 筛选标签
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

declare (strict_types=1);

namespace app\index\taglib\huicms;

use app\common\model\ModelsField;

class TagFilter extends Base
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 获取筛选列表
     *
     * @param $modelId
     * @param $tag
     *
     * @return array
     */
    public function lists($modelId, $tag)
    {
        $param = app()->request->get();

        // 获取允许的筛选字段
        $data = ModelsField::where(['model_id' => $modelId]);
        if ($tag['field']) { // 限制字段
            $data = $data->whereIn('name', $tag['field']);
        }
        $data   = $data->order('sort', 'asc')->select()->toArray();
        $filter = [];
        $field  = []; // 筛选字段

        foreach ($data as $key => $value) {
            $temp          = [];
            $temp['title'] = $value['title'];
            $temp['name']  = $value['name'];

            if (empty($value['value'])) {
                continue;
            }

            $arr = [];
            if ( ! empty($value['value'])) {
                $setting = json_decode($value['value'], true);
                $arr     = $setting;
            }

            $field[]   = $value['name'];
            $tempParam = $param;
            unset($tempParam[$value['name']]);

            // alltxt=off 关闭默认第一个选项
            if ($tag['alltxt'] != 'off') {
                $arr = array_merge([['name' => $tag['alltxt'], 'value' => '']], $arr);
            }
            foreach ($arr as $k => $v) {
                $tempOption['title'] = $v['name'];
                $tempOption['name']  = $v['value'];

                if ($tag['multiple'] == 1 && ! empty($param[$value['name']])) { // 允许多选
                    $val = explode(',', $param[$value['name']]);
                    if (in_array($v['value'], $val)) {
                        $val = array_diff($val, [$v['value']]);
                    } else {
                        $val[] = $v['value'];
                    }

                    $cur                        = [$value['name'] => implode(',', array_unique($val))];
                    $tempOption['currentstyle'] = isset($param[$value['name']]) && in_array($v['value'],
                        explode(',', $param[$value['name']])) ? $tag['currentstyle'] : '';
                } else {
                    $cur                        = [$value['name'] => $v['value']];
                    $tempOption['currentstyle'] = isset($param[$value['name']]) && $param[$value['name']] == $v['value'] ? $tag['currentstyle'] : '';
                }

                $tempOption['url'] = '?'.http_build_query(array_merge($param, $cur));
                $temp['option'][]  = $tempOption;
            }
            $filter[] = $temp;
        }

        return $filter;
    }
}
